/*Copyright (C) 2009 Careil Baptiste

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Network.h"
#ifdef WIN32
# include <winsock2.h>
# undef ERROR
# include <stdio.h>

unsigned int nbSockOpen = 0;

void InitWSA()
{
  static WSADATA wsa;
  if (nbSockOpen == 0)
    {
      if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
	{
	  perror("CSLP: Error when initializing WSA. The sockets can't be used.\n");
	}
      else
	{
	  nbSockOpen++;
	}
    }
  else
    {
      nbSockOpen++;
    }
}

void CloseWSA()
{
  nbSockOpen--;
  if (nbSockOpen == 0)
    {
      if (WSACleanup() != 0)
	{
	  perror("CSLP: Error when closing WSA.\n");
	}
    }
}
#else /* WIN32 */
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
#endif /* !WIN32 */

char DLL_IMPEXP * CSLP_ULongIPToString(unsigned long IP)
{
  struct in_addr sin;
  sin.s_addr = IP;
  return inet_ntoa(sin);
}

unsigned long DLL_IMPEXP CSLP_StringIPToULong(const char* IP)
{
  return inet_addr(IP);
}

unsigned long DLL_IMPEXP CSLP_ULongToBigEndian(unsigned long uLong)
{
  return htonl(uLong);
}

